package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.MinioFile;
import com.qzct.entity.vo.ResultCode;
import com.qzct.entity.vo.ResultVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.Banner;
import com.qzct.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2025-01-04
 */
@RestController
@Api(tags = "轮播图")
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;


    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键删除")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return bannerService.removeById(id);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<Banner> list() {
        return bannerService.list();
    }



    @PostMapping(value = "/upload/image",consumes = "multipart/form-data")
    @ApiOperation("轮播图上传")
    public ResultVo imageUploader(@RequestPart(value = "image") MultipartFile image){
        String fileName=image.getOriginalFilename();
        Banner banner = bannerService.upLoadImg("banners",fileName,image);
        if(banner==null){
            return ResultVo.error(ResultCode.FILE_UPLOAD_ERROR);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",banner);
        return  new ResultVo(banner);
    }

}
